<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item   label='项目' :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select label-in-value :show-search="showSearch" placeholder='请输入项目' :filter-option="filterOption" style='width: 300px;margin-right: 20px' v-decorator="['projectName',{rules:[{type:'string',required:true,message:'请选择项目！'}]}]" @change='initAreaList'>
            <a-select-option v-for='item in projectList' :label='item.name' :value='item.id' :key='item.id'>
              <span style="float: left">{{item.name}}</span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label='区域'  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select label-in-value :show-search="showSearch" placeholder='请输入项目' :filter-option="filterOption" style='width: 300px;margin-right: 20px' v-decorator="['areaName',{rules:[{type:'string',required: true,message:'请选择区域'}]}]" @change='initBuildList'>
            <a-select-option v-for='item in areaList' :label='item.name' :value='item.id' :key='item.id'>
              <span style="float: left">{{item.name}}</span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label='楼栋'    :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select label-in-value :show-search="showSearch" :filter-option="filterOption" placeholder='可为空' style='width: 300px;margin-right: 20px' v-decorator="['buildName',{}]" @change='initUnitList'>
            <a-select-option v-for='item in buildList' :label='item.buildingName' :value='item.id' :key='item.id'>
              <span style="float: left">{{item.buildingName}}</span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item  label='单元'    :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select label-in-value :show-search="showSearch" :filter-option="filterOption" placeholder='可为空' style='width: 300px;margin-right: 20px' v-decorator="['unitName',{}]" @change='initFloorList'>
            <a-select-option v-for='item in unitList' :label='item.unitName' :value='item.id' :key='item.id'>
              <span style="float: left">{{item.unitName}}</span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label='楼层'   :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select label-in-value :show-search="showSearch" :filter-option="filterOption" placeholder='可为空' style='width: 300px;margin-right: 20px' v-decorator="['floor',{}]" @change='setFloorId'>
            <a-select-option v-for='item in floorList' :label='item.floorName' :value='item.id' :key='item.id'>
              <span style="float: left">{{item.floorName}}</span>
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备类型编码">
          <a-input placeholder="请输入设备类型编码" v-decorator="['deviceCode', {rules:[{type:'string',required: true,message:'请选择区域'}]}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备型号">
          <a-input placeholder="请输入设备型号" v-decorator="['deviceModel', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备名称">
          <a-input placeholder="请输入设备名称" v-decorator="['deviceName', {rules:[{type:'string',required: true,message:'请选择区域'}]}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备名称">
          <a-input placeholder="请输入设备别名" v-decorator="['deviceName', {rules:[{type:'string',required: true,message:'请选择区域'}]}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备编号">
          <a-input-number v-decorator="[ 'deviceNum', validatorRules.deviceNum ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备sn号">
          <a-input placeholder="请输入设备sn号" v-decorator="['deviceSn', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设备版本号">
          <a-input placeholder="请输入设备版本号" v-decorator="['deviceVersion', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="通讯地址">
          <a-input placeholder="请输入通讯地址" v-decorator="['swapAddress', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="通讯方式">
          <a-input placeholder="请输入通讯方式" v-decorator="['swapType', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
import { getProjectList } from '@api/api'

  export default {
    name: "DeviceModal",
    props:[
      'deviceInfo'
    ],
    data () {
      return {
        regionName:"",
        projectName:"",
        areaName:"",
        buildName:"",
        unitName:"",
        floorName:"",
        houseName:"",
        projectList:[],
        areaList:[],
        buildList:[],
        unitList:[],
        floorList:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        deviceNum:{rules: [{ required: true, message: '请输入设备编号!' }]},
        projectRegionId:{rules: [{ required: true, message: '请输入项目区域id!' }]},
        },
        url: {
          add: "/device/add",
          edit: "/device/device/edit",
          getAreaInfos:'/area/getAreaList',
          getbuildInfos:'/building/getBuildList',
          getUnitInfos:'/unit/getUnitList',
          getFloorInfos:'floor/getFloorList',
          addHouse:'house/addHouse'
        },
      }
    },
    created () {
      debugger
    },
    methods: {
      filterOption(input, option) {
        let com = option.componentOptions
        let parkCode = String(com.propsData.value)
        let parkName = String(com.children[0].text)
        if(parkCode.toLowerCase().includes(input.toLowerCase())||parkName.toLowerCase().includes(input.toLowerCase())){
          return true
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createUser','deviceAlias','deviceCode','deviceModel','deviceName','deviceNum','deviceSn','deviceVersion','machineNum','projectName','projectRegionId','projectRegionName','swapAddress','swapType'))
		  //时间格式化
          this.form.setFieldsValue({updateTime:this.model.updateTime?moment(this.model.updateTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);

            //时间格式化
            formData.updateTime = formData.updateTime?formData.updateTime.format('YYYY-MM-DD HH:mm:ss'):null;

            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      initProjectList() {
        getProjectList().then((res)=>{
          if(res.success){
            this.projectList = res.result
          }
        })
      },
      initAreaList(e) {
        this.projectName = e.label[0].children[0].text
        let params = {
          projectId:e.key
        }
        this.form.resetFields(['areaName','buildName','unitName','floor'])
        getAction(this.url.getAreaInfos,params).then((res)=>{
          if(res.success){
            this.areaList = res.result
          }
        })
      },
      initBuildList(e) {
        this.areaName = e.label[0].children[0].text
        this.form.resetFields(['buildName','unitName','floor'])
        let params = {
          areaId:e.key
        }
        getAction(this.url.getbuildInfos,params).then((res)=>{
          if(res.success){
            this.buildList = res.result
          }
        })
      },
      initUnitList(e) {
        this.buildName = e.label[0].children[0].text
        this.form.resetFields(['unitName','floor'])
        let params = {
          buildingId:e.key
        }
        getAction(this.url.getUnitInfos,params).then((res)=>{
          if(res.success){
            this.unitList = res.result

          }
        })
      },
      initFloorList(e) {
        this.unitName = e.label[0].children[0].text
        this.form.resetFields(['floor'])
        let params = {
          unitId:e.key
        }
        this.floorId=""
        getAction(this.url.getFloorInfos,params).then((res)=>{
          if(res.success){
            this.floorList = res.result
          }
        })
      },
      setFloorId(e){
        this.floorName = e.label[0].children[0].text
        this.floorId=e.key
      },
      setHouseName(e){
        this.houseName=ee.label[0].children[0].text
      },
    }
  }
</script>

<style lang="less" scoped>

</style>